/*
day482 
2020.12.22 
*/
#include<iostream>
#include<vector>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    TreeNode* sortedListToBST(ListNode* head) {
        vector<int> vec;
        while(head)
        {
        	vec.push_back(head->val);
        	head=head->next;
		}
		
		return help(vec,0,vec.size()-1);
    }
    
    TreeNode* help(vector<int> &vec,int l,int r)
    {
    	if(l>r) return nullptr;
    	
    	if(l==r)
    	{
    		TreeNode *root=new TreeNode(vec[l]);
			root->left=nullptr;
			root->right=nullptr;
			return root;
		}
		 
		int mid=(l+r)/2;
		TreeNode *root=new TreeNode(vec[mid]);
		root->left=help(vec,l,mid-1);
		root->right=help(vec,mid+1,r);
		return root;
	}
};


int main()
{
	return 0;
}

